from django import forms
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.core.exceptions import MultipleObjectsReturned
from django.contrib.auth import forms as auth_forms
from django.contrib.auth import password_validation
from . import models


class AuthorizationPageForm(auth_forms.AuthenticationForm):

    username = forms.CharField(
            widget=forms.TextInput(attrs={
                "type": "text", "id": "user", "name": "user", "placeholder": "username"}))

    password = forms.CharField(widget=forms.PasswordInput(attrs={
        "type": "password", "id": "pass", "name": "pass", "placeholder": "password"}))

    class Meta:

        model = User
        fields = ['username', 'password']



class PasswordChangePageForm(auth_forms.PasswordChangeForm):

    old_password = forms.CharField(required=True, label='Old password:',
            widget=forms.PasswordInput(attrs={
                "type": "password", "id": "pass", "name": "pass", "placeholder": "old password"}))

    new_password1 = forms.CharField(required=True, label='New password:',
            widget=forms.PasswordInput(attrs={
                "type": "password", "id": "pass", "name": "pass", "placeholder": "new password"}))

    new_password2 = forms.CharField(required=True, label='New password again:',
            widget=forms.PasswordInput(attrs={
                "type": "password", "id": "pass", "name": "pass", "placeholder": "new password"}))


class PasswordResetCustomForm(auth_forms.PasswordResetForm):

    email = forms.EmailField(required=True, label='Your email: ', max_length=254, widget=forms.EmailInput(attrs={
        "type": "email", "placeholder": 'email@example.com'}))


class PasswordResetConfirmCustomForm(auth_forms.SetPasswordForm):

    new_password1 = forms.CharField(
        label=("New password"),
        widget=forms.PasswordInput(attrs={"type": "password", "id": "pass", "name": "pass", "placeholder": "new password"}),
        strip=False,
        help_text=password_validation.password_validators_help_text_html(),
    )

    new_password2 = forms.CharField(
        label=("New password confirmation"),
        strip=False,
        widget=forms.PasswordInput(attrs={"type": "password", "id": "pass", "name": "pass", "placeholder": "new password"}),
    )


class RegistrationPageForm(forms.ModelForm):

    username = forms.CharField(label='Username:', widget=forms.TextInput(attrs={
                "type": "text", "id": "user", "name": "user", "placeholder": "username"}))

    email = forms.EmailField(required=True, label='Your email: ', max_length=254, widget=forms.EmailInput(attrs={
        "type": "email", "placeholder": 'example@gmail.com'}))

    password = forms.CharField(label='Password:', widget=forms.PasswordInput(attrs={
        "type": "password", "id": "pass", "name": "pass", "placeholder": "password"}))

    password2 = forms.CharField(label='Password again:', widget=forms.PasswordInput(attrs={
        "type": "password", "id": "pass", "name": "pass", "placeholder": "password"}))

    class Meta:

        model = User
        fields = ('username', 'email')


    def clean_password2(self):

        cd = self.cleaned_data

        if cd['password'] != cd['password2']:

            raise ValidationError('Password dont\' match.')

        return cd['password2']


class UserEdit(forms.ModelForm):

    first_name = forms.CharField(required=False, label='Name', widget=forms.TextInput(attrs={
        "type": "text", "id": "user", "name": "user", "placeholder": "name"}))

    last_name = forms.CharField(required=False, label='Surname', widget=forms.TextInput(attrs={
        "type": "text", "id": "user", "name": "user", "placeholder": "surname"}))

    email = forms.EmailField(required=False, label='Email', widget=forms.EmailInput(attrs={
        "type": "text", "id": "user", "name": "user", "placeholder": "email"}))

    class Meta:

        model = User
        fields = ['first_name', 'last_name']


class ProfileEdit(forms.ModelForm):

    photo = forms.URLField(required=False, label='Photo', widget=forms.URLInput(attrs={
        'type': 'text', 'placeholder': 'https://example.com/image.jpg'}))

    favorite_phrase = forms.CharField(required=False, label='Favorite phrase', widget=forms.TextInput(attrs={
        "type": "text", "id": "user", "name": "user", "placeholder": "favorite phrase"}))
    
    date_of_birth = forms.CharField(required=False, label='Date of birth', widget=forms.TextInput(attrs={
        "type": "text", "id": "user", "name": "user", "placeholder": "YYYY-MM-DD"}))

    class Meta:

        model = models.Profile
        fields = ['favorite_phrase', 'date_of_birth']
